// Copyright 2015 Christian Roggia. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.

#include "Time.h"

BOOL Shamoon::Utils::Time::IsBissextileYear(INT32 nYear)
{
	bool bIsBissextile;

	if(nYear <= 0)
		return FALSE;
	
	bIsBissextile = (nYear & 0x80000003) == 0;
	
	if((nYear & 0x80000003) < 0)
		bIsBissextile = (((nYear & 0x80000003) - 1) | 0xFFFFFFFC) == -1;
	
	return (bIsBissextile && (nYear % 100 || !(nYear % 400))) ? TRUE : FALSE;
}

DWORD g_nDaysInMonth[] =
{
	31, 28, 31, 30,
	31, 30, 31, 31,
	30, 31, 30, 31
};

INT32 Shamoon::Utils::Time::GetDaysInMonth(INT32 nYear, INT32 nMonth)
{
	INT32 nDays;

	if((UINT32)(nMonth - 1) > 11)
		return 0;
	
	nDays = g_nDaysInMonth[nMonth];
	if(nMonth == 2)
	{
		if(IsBissextileYear(nYear))
			++nDays;
	}

	return nDays;
}